package waylines.actions
{
	import org.flintparticles.twoD.particles.Particle2D;
	import flash.geom.Point;
	import org.flintparticles.common.particles.Particle;
	import org.flintparticles.common.emitters.Emitter;
	import org.flintparticles.common.actions.ActionBase;

	public class Antigravities extends ActionBase
	{
		private var fix:Emitter;
		private var power:Number;
		
		public function Antigravities(fix:Emitter, power:Number = -50000)
		{
			this.fix = fix;
			this.power = power;
			priority = -6;
		}
		override public function update( emitter:Emitter, particle:Particle, time:Number ):void
		{
			var _epsilonSq:Number = 10;
			
			var bigs:Array = fix.particlesArray;
			var length:int = bigs.length;
			var pointBig:Point = new Point();
			var pointPart:Point = new Point(Particle2D(particle).x, Particle2D(particle).y);
			for(var i:int=0; i<length; i++)
			{
				pointBig.x = Particle2D(bigs[i]).x;
				pointBig.y = Particle2D(bigs[i]).y;
				if(Point.distance(pointBig, pointPart) < 50)
				{
					var p:Particle2D = Particle2D( particle );
					var x:Number = pointBig.x - p.x;
					var y:Number = pointBig.y - p.y;
					var dSq:Number = x * x + y * y;
					if( dSq == 0 )
					{
						return;
					}
					var d:Number = Math.sqrt( dSq );
					if( dSq < _epsilonSq ) dSq = _epsilonSq;
					var factor:Number = ( power * time ) / ( dSq * d );
					
					p.velX += x * factor;
					p.velY += y * factor;
				}
				
				
			}
			
		}
	}
}
